package linkedlist.leetcode_445;

import java.util.Deque;
import java.util.Stack;

/**
 * 该方法不符合要求，但是思路很重要。
 */
public class ReverseSolution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        l1 = reverse(l1);
        l2 = reverse(l2);
        int addOne = 0;
        ListNode ans = null;
        while(l1 != null || l2 != null){
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;
            int sum = x + y + addOne;
            addOne = sum / 10;
            sum = sum % 10;
            ListNode cur = new ListNode(sum);
            cur.next = ans;
            ans = cur;
            if(l1 != null){
                l1 = l1.next;
            }
            if(l2 != null){
                l2 = l2.next;
            }
        }
        if(addOne > 0){
            ListNode head = new ListNode(addOne);
            head.next = ans;
            ans = head;
        }
        return ans;
    }
    public ListNode reverse(ListNode head){
        ListNode pre = null;
        while(head != null){
            ListNode temp = head.next;
            head.next = pre;
            pre = head;
            head = temp;
        }
        return pre;
    }
}
